#include <QMessageBox>
#include <QtPlugin>

#include "ftp.h"

QString FtpCodec::escape(const char &ch)
{
	if ( (ch == 13) || (ch == 10) ) return QString("<br/>");
	if ( ch < 32 ) return QString(".");
	if ( ch == '<' ) return QString("&lt;");
	if ( ch == '>' ) return QString("&gt;");
	if ( ch == '&' ) return QString("&amp;");
	if ( ch == '\"' ) return QString("&quot;");
	if ( ch == '\'' ) return QString("&apos;");

	return QString(ch);
}

FtpCodec::FtpCodec()
{
	m_ToServerColor = QColor("red");
	m_ToClientColor = QColor("green");
}

FtpCodec::~FtpCodec()
{
}

// decode data
QString FtpCodec::decode(const QByteArray& ba, const bool toServer)
{
	QString str;
	for (int i=0; i<ba.size(); ++i)
		str += escape( ba.at(i) );
	
	// TODO
	/*
	QRegExp rx("^[0-9]{1,3}")
	if ( rx.indexIn(str) == 0 )
	*/		
	
	QString num, rest;
	bool spaceWas = false;
	for (int i=0; i<ba.size(); ++i)
	{
		if ( ba.at(i) == ' ' ) spaceWas = true;
		
		if (spaceWas)
			rest += escape(ba.at(i));
		else
			num += escape(ba.at(i));
	}
	

	QString ret = QString("<b><u>") + num + QString("</u></b>") + rest;
	
	if (toServer)	ret = QString("<font color=\"%1\">").arg(m_ToServerColor.name()) + ret + "</font>";
	else 		ret = QString("<font color=\"%1\">").arg(m_ToClientColor.name()) + ret + "</font>";
	
	return ret;
}

// encode data for send
QByteArray FtpCodec::encode(const QString& str, const bool toServer)
{
	//TODO
	QByteArray ba;
	return ba;
}

// returns codec short name for menu
QString FtpCodec::codecName()
{
	return QString("ftp");
}

// return codec description
QString FtpCodec::codecDesc()
{
	return QString("ftp codec");
}

TParams FtpCodec::params()
{
	TParams t;
	return t;
}

void FtpCodec::setParams(TParams&)
{
	// ignore
}

void FtpCodec::showParamsDialog()
{
	QString text = "This is a <b>ftp</b> codec.";
	QMessageBox::about(0, "ftp", text);
}

// Change for your file name (TARGET in .pro) and class name
Q_EXPORT_PLUGIN2(ftp, FtpCodec);

// kate: space-indent off; tab-indents on; indent-width 4; indent-mode cstyle;
